package com.lc.pianostudent.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.activity.ConstellationActivity;
import com.lc.pianostudent.activity.EverydayAnswerActivity;
import com.lc.pianostudent.activity.MyWorldActivity;
import com.lc.pianostudent.activity.ServiceActivity;
import com.lc.pianostudent.activity.StudyActivity;
import com.lc.pianostudent.activity.VideoWebActivity;
import com.lc.pianostudent.activity.WebActivity;
import com.lc.pianostudent.adapter.HomeNewsAdapter;
import com.lc.pianostudent.adapter.PianoTimeAdapter;
import com.lc.pianostudent.bean.HomeNewsBean;
import com.lc.pianostudent.bean.PianoTimeBean;
import com.lc.pianostudent.conn.PostGetBanner;
import com.lc.pianostudent.conn.PostGetuser;
import com.lc.pianostudent.view.ScrollLinearLayoutManager;
import com.lc.pianostudent.widget.FullyLinearLayoutManager;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/18.
 */

public class HomeFragment extends BaseFragment implements View.OnClickListener{
    private List<String> bannerlist = new ArrayList<>();
    @BoundView(R.id.item_home_advert) private Banner banner;
    private List<HomeNewsBean> newslist = new ArrayList<>();


    @BoundView(R.id.home_news_recycleview) private RecyclerView recyclerView;
    private HomeNewsAdapter homeNewsAdapter;
    @BoundView(value = R.id.read_more,isClick = true) private TextView read_more;
    @BoundView(value = R.id.my_world_layout,isClick = true) private LinearLayout my_world_layout;
    @BoundView(value = R.id.constellation_layout,isClick = true) private LinearLayout constellation_layout;
    @BoundView(value = R.id.everyday_answer_layout,isClick = true) private LinearLayout everyday_answer_layout;
    @BoundView(value = R.id.study_layout,isClick = true) private LinearLayout study_layout;
    private PostGetBanner.PostGetBannerInfo currentinfo;

    private PostGetBanner postGetBanner = new PostGetBanner(new AsyCallBack<PostGetBanner.PostGetBannerInfo>() {


        @Override
        public void onSuccess(String toast, int type, Object o, PostGetBanner.PostGetBannerInfo postGetBannerInfo) throws Exception {
//            Log.e("===jy",postGetBannerInfo.bannerList.get(0)+"---");
            currentinfo = postGetBannerInfo;
            //轮播图
            banner .setImages(postGetBannerInfo.bannerList)//设置数据源
                    .setImageLoader(new MyLoader())//设置图片加载器
                    .setDelayTime(3000)
                    .start();//开始滚动




            if(type == 0){
                newslist.clear();
            }


            newslist.addAll(postGetBannerInfo.homeNewsList);
            homeNewsAdapter.notifyDataSetChanged();

            if(currentinfo.current_page*currentinfo.per_page<currentinfo.total){
                read_more.setVisibility(View.VISIBLE);
            }else {
                read_more.setVisibility(View.GONE);
            }
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = BoundViewHelper.boundView(this, inflater.inflate(R.layout.fragment_home, null));

        initView();
        setAppCallBack(new CallBack());
        return  view;
    }

    private void initView(){
        read_more.setVisibility(View.INVISIBLE);
        FullyLinearLayoutManager mLayoutManager = new FullyLinearLayoutManager(getContext());
        recyclerView.setLayoutManager(mLayoutManager);
//        ScrollLinearLayoutManager scrollLinearLayoutManager = new ScrollLinearLayoutManager(getContext());

        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Log.e("====",position+"=====");
                startActivity(new Intent(getContext(), WebActivity.class).putExtra("url", currentinfo.linkUrl.get(position)).putExtra("title", ""));

            }
        });
        homeNewsAdapter = new HomeNewsAdapter(getContext(), newslist);
        recyclerView.setAdapter(homeNewsAdapter);
        postGetBanner.page = "1";
        postGetBanner.type = "2";
        postGetBanner.execute();

        homeNewsAdapter.setOnItemClickListener(new HomeNewsAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(newslist.get(position).getClassid().equals("2") ||newslist.get(position).getClassid().equals("0")){
//                    startActivity(new Intent(getContext(), VideoWebActivity.class).putExtra("url", newslist.get(position).getUrl()).putExtra("title", newslist.get(position).getTitle()));
                    Intent intent = new Intent();

                    intent.setAction("android.intent.action.VIEW");

                    Uri content_url = Uri.parse(newslist.get(position).getUrl());

                    intent.setData(content_url);

                    startActivity(intent);
                }else {
                    startActivity(new Intent(getContext(), WebActivity.class).putExtra("url", newslist.get(position).getUrl()).putExtra("title", newslist.get(position).getTitle()));

                }

            }
        });


//        ScrollLinearLayoutManager scrollLinearLayoutManager = new ScrollLinearLayoutManager(this);
//        scrollLinearLayoutManager.setScrollEnabled(false);
//        mRecyclerView.setLayoutManager(scrollLinearLayoutManager);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.read_more:
                if(currentinfo.current_page*currentinfo.per_page<currentinfo.total){
                    postGetBanner.page = currentinfo.current_page+1+"";
                    postGetBanner.type = "2";
                    postGetBanner.execute(false,1);
                    homeNewsAdapter.notifyDataSetChanged();
                }else {
                    read_more.setVisibility(View.GONE);
                }

                break;

            case R.id.my_world_layout:
                startVerifyActivity(MyWorldActivity.class);
                break;

            case R.id.constellation_layout:
                startVerifyActivity(ConstellationActivity.class);
                break;

            case R.id.everyday_answer_layout:
                startVerifyActivity(EverydayAnswerActivity.class);
                break;

            case R.id.study_layout:
                startVerifyActivity(StudyActivity.class);
                break;
        }
    }

    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
//            Glide.with(context).load((String) path).into(imageView);
            GlideLoader.getInstance().get((String) path,imageView,R.mipmap.banner);
        }
    }
    public class CallBack implements AppCallBack {

        public void onRefresh() {


            postGetBanner.page = "1";
            postGetBanner.type = "2";
            postGetBanner.execute(getContext(),false,0);

        }

    }
}
